Imports Microsoft.VisualBasic
Imports System.Configuration
Imports System.Data
Imports System.Data.SqlClient
Imports System.Collections.Generic
Imports EntityLayer

Public Class ProductoProveedorDA

   
    Public Shared Function Insertar(ByVal OBE As ProductoProveedorBE) As Boolean
        Try
            Dim myConnection As New SqlConnection(ConexionDA.ObtenerCnx)
            Dim myCommand As New SqlCommand("COM_ProductoProveedor_Add", myConnection)
            myCommand.CommandType = CommandType.StoredProcedure

            Dim para As SqlParameter

            para = New SqlParameter("@strDescripcion", SqlDbType.VarChar, 100)
            para.Direction = ParameterDirection.Input
            para.Value = OBE.Descripcion
            myCommand.Parameters.Add(para)

            para = New SqlParameter("@strCodigo", SqlDbType.VarChar, 5)
            para.Direction = ParameterDirection.Input
            para.Value = OBE.Codigo
            myCommand.Parameters.Add(para)


            para = New SqlParameter("@strCodigoProducto", SqlDbType.VarChar, 5)
            para.Direction = ParameterDirection.Input
            para.Value = OBE.CodigoProducto
            myCommand.Parameters.Add(para)

            myConnection.Open()
            myCommand.ExecuteNonQuery()
            myConnection.Close()

            Return True
        Catch ex As Exception
            Dim E As String = ex.Message
            Return False
        End Try

    End Function

    Public Shared Function Eliminar(ByVal strCodigo As String) As Boolean
        Try
            Dim myConnection As New SqlConnection(ConexionDA.ObtenerCnx)
            Dim myCommand As New SqlCommand("COM_ProductoProveedor_Del", myConnection)
            myCommand.CommandType = CommandType.StoredProcedure

            Dim para As SqlParameter

            para = New SqlParameter("@strCodigo", SqlDbType.VarChar, 5)
            para.Direction = ParameterDirection.Input
            para.Value = strCodigo
            myCommand.Parameters.Add(para)

            myConnection.Open()
            myCommand.ExecuteNonQuery()
            myConnection.Close()

            Return True
        Catch ex As Exception
            Dim E As String = ex.Message
            Return False
        End Try

    End Function

    Public Shared Function ListarxCodigo(ByVal strCodigo As String) As DataSet
        Dim myConnection As New SqlConnection(ConexionDA.ObtenerCnx)
        Dim myDataadapter As New SqlDataAdapter("COM_ProductoProveedor_ListByCodigo", myConnection)
        myDataadapter.SelectCommand.CommandType = CommandType.StoredProcedure

        Dim ds As New DataSet

        Dim para As SqlParameter

        para = New SqlParameter("@strCodigo", SqlDbType.VarChar, 5)
        para.Direction = ParameterDirection.Input
        para.Value = strCodigo
        myDataadapter.SelectCommand.Parameters.Add(para)

        myDataadapter.Fill(ds, "ProductoProveedor")

        Return ds

    End Function

    Public Shared Function ListarTodo() As DataSet
        Dim myConnection As New SqlConnection(ConexionDA.ObtenerCnx)
        Dim myDataadapter As New SqlDataAdapter("COM_ProductoProveedor_ListAll", myConnection)
        myDataadapter.SelectCommand.CommandType = CommandType.StoredProcedure

        Dim ds As New DataSet

        myDataadapter.Fill(ds, "ProductoProveedor")

        Return ds

    End Function

    Public Shared Function ListarProductosconProveedor() As List(Of Producto)

        Dim myConnection As New SqlConnection(ConexionDA.ObtenerCnx)
        Dim myCommand As New SqlCommand("COM_ProductoProveedor_ListconProveedor", myConnection)
        myCommand.CommandType = CommandType.StoredProcedure

        myConnection.Open()

        Dim reader As SqlDataReader = myCommand.ExecuteReader(CommandBehavior.CloseConnection)

        Dim Productos As New List(Of Producto)

        While reader.Read()
            Dim rsProducto As New Producto
            rsProducto.Codigo = reader("CodigoProducto").ToString
            rsProducto.Nombre = reader("NombreProducto").ToString
            rsProducto.UnidadMedida = reader("UnidadMedida").ToString
            rsProducto.FechaIngreso = reader("FechaIngreso")
            rsProducto.PrecioUnitario = Convert.ToDecimal(reader("PrecioUnitario"))
            Productos.Add(rsProducto)
        End While

        reader.Close()
        myConnection.Close()

        Return Productos

    End Function

    Public Shared Function ListarProductosconProveedor(ByVal strBusqueda As String) As List(Of Producto)

        Dim myConnection As New SqlConnection(ConexionDA.ObtenerCnx)
        Dim myCommand As New SqlCommand("COM_ProductoProveedor_BuscarconProveedor", myConnection)
        myCommand.CommandType = CommandType.StoredProcedure

        myConnection.Open()

        Dim para As SqlParameter

        para = New SqlParameter("@strBusqueda", SqlDbType.VarChar, 100)
        para.Direction = ParameterDirection.Input
        para.Value = strBusqueda
        myCommand.Parameters.Add(para)

        Dim reader As SqlDataReader = myCommand.ExecuteReader(CommandBehavior.CloseConnection)

        Dim Productos As New List(Of Producto)

        While reader.Read()
            Dim rsProducto As New Producto
            rsProducto.Codigo = reader("CodigoProducto").ToString
            rsProducto.Nombre = reader("NombreProducto").ToString
            rsProducto.UnidadMedida = reader("UnidadMedida").ToString
            rsProducto.FechaIngreso = reader("FechaIngreso")
            rsProducto.PrecioUnitario = Convert.ToDecimal(reader("PrecioUnitario"))
            Productos.Add(rsProducto)
        End While

        reader.Close()
        myConnection.Close()

        Return Productos

    End Function
End Class
